import Vue from 'vue';
import Component from '../message';
const SuccessConstructor = Vue.extend(Component);
const type = 'error'
Component.name = 'message-' + type;
const error = function (options) {
  const {
    callback,
    ...rest
  } = options
  let time = options.time || 2000
  if (Vue.prototype.$isServer) return
  options = options || {}
  const id = 'message-' + type + 1;
  const instance = new SuccessConstructor({
    propsData: {
      ...rest
    }
  })
  instance.id = id
  instance.vm = instance.$mount()
  document.body.appendChild(instance.vm.$el)
  instance.vm.show = true;
  instance.vm.type = type;
  instance.vm.name = 'message-' + type;
  instance.vm.$on('close', () => {
    instance.vm.show = false;
  })
  setTimeout(() => {
    if (typeof callback === 'function') {
      callback(instance.vm);
    }
    instance.vm.close();
  }, time);
  return instance.vm
}
export default error
